﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace One.Framework.Util
{
    /// <summary>
    /// 레지스트리
    /// </summary>
    public class RegistryHelper
    {
        /// <summary>
        /// 레지스트리 존재 여부
        /// </summary>
        /// <param name="path">레지스트리 경로</param>
        /// <returns>True/False</returns>
        public static bool CheckRegistry(string path)
        {
            try
            {
                Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path);

                if (regkey == null)
                {
                    return false;
                }
                else
                {
                    return true;
                }
            }
            catch
            {
                return false;
            }
        }

        /// <summary>
        /// 레지스트리 조회
        /// </summary>
        /// <param name="path">레지스트리 경로</param>
        /// <param name="key">키명</param>
        /// <returns>레지스트리 반환</returns>
        public static string ReadRegistry(string path, string key)
        {
            try
            {
                Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path);

                string RegValue = regkey.GetValue(key).ToString();

                if (string.IsNullOrEmpty(RegValue))
                {
                    return null;
                }
                else
                {
                    return RegValue;
                }
            }

            catch (Exception ex)
            {
                return "No Entry";
            }
        }

        /// <summary>
        /// 레지스트리 수정
        /// </summary>
        /// <param name="path">레지스트리 경로</param>
        /// <param name="key">키명</param>
        /// <param name="value">값</param>
        /// <returns>수정완료시 true</returns>
        public static bool EditRegistry(string path, string key, string value)
        {
            try
            {
                Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path, true);

                System.Security.AccessControl.RegistrySecurity rs = regkey.GetAccessControl(System.Security.AccessControl.AccessControlSections.Access);

                rs.SetGroup(new System.Security.Principal.NTAccount("Administrators"));
                rs.SetOwner(new System.Security.Principal.NTAccount("Administrators"));
                rs.SetAccessRuleProtection(false, false);

                regkey.SetAccessControl(rs);
                regkey.SetValue(key, value, Microsoft.Win32.RegistryValueKind.DWord);

                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 레지스트리 생성
        /// </summary>
        /// <param name="path">레지스트리 생성 경로</param>
        /// <param name="key">키명</param>
        /// <param name="value">값</param>
        /// <returns>bool</returns>
        public static bool WriteRegistry(string path, string key, string value)
        {
            try
            {
                Microsoft.Win32.Registry.LocalMachine.CreateSubKey(path);
                Microsoft.Win32.RegistryKey regkey = Microsoft.Win32.Registry.LocalMachine.OpenSubKey(path, true);
                regkey.SetValue(key, value);

                return true;
            }
            catch (Exception ex)
            {
                return false;
            }
        }

        /// <summary>
        /// 레지스트리 삭제
        /// </summary>
        /// <param name="path">레지스트리 경로</param>
        /// <returns>삭제 여부</returns>
        public static bool DeleteRegistry(string path)
        {
            try
            {
                Microsoft.Win32.Registry.LocalMachine.DeleteSubKey(path);
                return true;
            }

            catch (Exception ex)
            {
                return false;
            }
        }
    }
}
